class Person < ActiveRecord::Base
  
  belongs_to :city
  has_one :member
  
  has_one :people_license
  has_one :license, :through => :people_license
  
  has_many :people_licenses
  has_many :licenses, :through => :people_licenses
  
  #wykorzystywane przy edycji osoby (pozwala operowac na person.rally_license): 
  has_one :rally_license, :through => :people_licenses, :source => :license
  has_one :referee_license, :through => :people_licenses, :source => :license
  
  #wszystkie licencje kolesioof
  has_many :rally_licenses, 
    :through => :people_licenses, 
    :source => :license, 
    :conditions => ["kind = ?", "rajdowa"], 
    :order => "created_at DESC"
  has_many :referee_licenses, 
    :through => :people_licenses, 
    :source => :license, 
    :conditions => ["kind = ?", "sędziowska"],
    :order => "created_at DESC"

  #najnowsze biezące licencje:
  has_one :current_rally_license, 
    :through => :people_licenses, 
    :source => :license, 
    :conditions => ["year = ? and kind = 'rajdowa'", DateTime.now.year], 
    :order => 'created_at DESC'
  has_one :current_referee_license, 
    :through => :people_licenses, 
    :source => :license, 
    :conditions => ["year = ? and kind = 'sędziowska'", DateTime.now.year], 
    :order => 'created_at DESC'
  
  validates_presence_of :first_name, :last_name
end
